package com.wechat.android.app.ui.message;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wechat.android.app.R;
import com.wechat.android.app.adapter.NewFriendAdapter;
import com.wechat.android.app.callback.RecycleViewDivider;
import com.wechat.android.app.db.UserHelper;
import com.wechat.android.app.entity.GetFriendRequestModel;
import com.wechat.android.app.global.Constants;
import com.wechat.android.app.rxbus.RxBus;
import com.wechat.android.app.rxbus.event.CommonEvent;
import com.wechat.android.app.ui.BaseActivity;
import com.wechat.android.app.utils.MyLog;
import com.wechat.android.app.utils.PhoneUtils;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by yang on 2018/1/16.
 */

public class NewFriendsMsgActivity extends BaseActivity {
    @BindView(R.id.rv_add_friend)
    RecyclerView rvAddFriend;

    private List<GetFriendRequestModel> getFriendRequestList = new ArrayList<>();
    private NewFriendAdapter newFriendAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.em_activity_new_friends_msg;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("\uD81B\uDF3D\uD81B\uDFD3\uD81B\uDF16\uD81B\uDF54\uD81B\uDF43\uD81B\uDFA0\uD81B\uDF2F\uD81B\uDF51\uD81B\uDFC7");
        getSubTitle().setText("\uD81B\uDF0A\uD81B\uDF51\uD81B\uDFC4\uD81B\uDFC0\uD81B\uDF3B\uD81B\uDFD4\uD81B\uDF3D\uD81B\uDFD3\uD81B\uDF16\uD81B\uDF54");
        newFriendAdapter = new NewFriendAdapter(R.layout.item_search_friend, getFriendRequestList);
        rvAddFriend.setLayoutManager(new LinearLayoutManager(NewFriendsMsgActivity.this));
        rvAddFriend.addItemDecoration(new RecycleViewDivider(NewFriendsMsgActivity.this, LinearLayoutManager.VERTICAL, R.drawable.shape_line_divider_decoration));
        rvAddFriend.setAdapter(newFriendAdapter);

        newFriendAdapter.setAddFriendOnClick(new NewFriendAdapter.OnAddFriendsListener() {
            @Override
            public void getAddFriendOnClick(GetFriendRequestModel data) {
                MyLog.e("yang","data.getFriendId()=="+data.getFriendId());
                addFriend(data.getFriendId() + "");
            }
        });

        getFriend();


    }

    @Override
    protected void updateViews() {

    }

    @OnClick(R.id.toolbar_subtitle)
    public void viewOnClick(View view) {
        startActivity(new Intent(NewFriendsMsgActivity.this, AddContactActivity.class));
    }

    //获取好友请求列表
    private void getFriend() {
        mMProgressDialog.showNoText();
        EasyHttp.post(Constants.API_GETFRIEND)
                .params("imKey", UserHelper.getInstance().getUserInfoModel(NewFriendsMsgActivity.this).getImKey())
                .timeStamp(true)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();

                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", "onError==" + e.toString());
                        mMStatusDialog.show("\uD81B\uDF1E\uD81B\uDFA9\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF2F\uD81B\uDF51\uD81B\uDF62\uD81B\uDF3D\uD81B\uDF54\uD81B\uDF16\uD81B\uDF60",null);
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "好友请求成功==" + result);
                        String data = PhoneUtils.checkResponseFlag(NewFriendsMsgActivity.this, result);
                        if (data != null) {
                            Gson gson = new Gson();
                            getFriendRequestList = gson.fromJson(data, new TypeToken<List<GetFriendRequestModel>>() {
                            }.getType());
                            newFriendAdapter.setNewData(getFriendRequestList);
                        }
                    }
                });
    }


    //    添加好友
    private void addFriend(String friendId) {
        mMProgressDialog.showNoText();
        HttpParams params = new HttpParams();
        params.put("friendId", friendId);
        params.put("flag", "true");
        EasyHttp.post(Constants.API_ADDFRIENDSINGLE)
                .params(params)
                .timeStamp(true)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", "onError==" + e.toString());
                        mMStatusDialog.show("\uD81B\uDF1E\uD81B\uDFA9\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF2F\uD81B\uDF51\uD81B\uDF62\uD81B\uDF3D\uD81B\uDF54\uD81B\uDF16\uD81B\uDF60",null);
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "添加好友==" + result);
                        String data = PhoneUtils.checkResponseFlag(NewFriendsMsgActivity.this, result);
                        if (data != null) {
                            //添加成功
                            MyLog.e("yang","添加好友成功");
                            mMStatusDialog.show("\uD81B\uDF0A\uD81B\uDF51\uD81B\uDFC4\uD81B\uDFC0\uD81B\uDF3B\uD81B\uDFD4\uD81B\uDF3D\uD81B\uDFD3\uD81B\uDF16\uD81B\uDF54",null);
                            RxBus.getDefault().post(Constants.RXBUS_CODE_REFRESH_CONTACT,new CommonEvent("刷新通讯录"));
                        }
                    }
                });
    }


}
